package com.zh.seckill.exception;

import com.zh.seckill.vo.R;
import com.zh.seckill.vo.RespEnum;
import org.springframework.validation.BindException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * 全局异常处理类
 *
 * @Author: zhanghuan
 * @date: 2022/3/28 18:40
 * @description: create it
 * //
 */
@RestControllerAdvice
public class GlobalExceptionHandler {

    @ExceptionHandler
    public R ExceptionHandler(Exception e) {
        if (e instanceof GlobalException) {
            GlobalException ex = (GlobalException) e;
            return R.error(ex.getRespEnum());

        } else if (e instanceof BindException) {
            BindException bindException = (BindException) e;
            R r = R.error(RespEnum.BIND_ERROR);
            r.setMessage("参数校验异常" + bindException.getAllErrors().get(0).getDefaultMessage());
            return r;

        }
        return R.error(RespEnum.ERROR);
    }
}
